package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        //1.根据用户id查找购物车
        //获取当前用户
        Long userID = BaseContext.getCurrentId();
       cart.setUserId(userID);
        //查询添加的商品是否存在
        ShoppingCart shoppingCart = shoppingCartMapper.findItemIsExit(cart);
        if(shoppingCart==null){
            //当前商品不存在
            if(shoppingCartDTO.getDishId()==null){
                //当前查询的是套餐
               Setmeal setmeal = setmealMapper.findByID(shoppingCartDTO.getSetmealId());
                cart.setName(setmeal.getName());
                cart.setAmount(setmeal.getPrice());
                cart.setImage(setmeal.getImage());
            }else {
                //当前查询的是菜品
                DishVO dishVO = dishMapper.getDishByID(shoppingCartDTO.getDishId());
                cart.setName(dishVO.getName());
                cart.setAmount(dishVO.getPrice());
                cart.setImage(dishVO.getImage());
            }
            cart.setCreateTime(LocalDateTime.now());
            cart.setNumber(1);
            shoppingCartMapper.add(cart);
        }else {
            //当前商品已存在购物车
            cart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }

    }

    /**
     * 查看购物车
     * @return shoppingCart
     */
    @Override
    public List<ShoppingCart> getSHoppingCartList() {
        List<ShoppingCart> shoppingCarts=shoppingCartMapper.getSHoppingCartList(BaseContext.getCurrentId());
        return shoppingCarts;
    }
    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.findItemIsExit(shoppingCart);
        if(cart.getNumber()>1){
            //只将数量减1即可
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.update(cart);
        }else {
            //将该条数据删除
           shoppingCartMapper.sub(cart);
        }
    }
    /**
     * 清空购物车
     */
    @Override
    public void clean() {

        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }
}
